/* $Header: pi_config.c,v 2.2 01/10/02 10:27:57 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:                                                        */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE:                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <i_op.h>
#include <p_status.h>
#include <list_mgmt.h>
#include <ramps.h>         /* needed for status.h */
#include <zone_config.h>   /* needed for status.h */
#include <zone_status.h>   /* needed for status.h */
#include <status.h>     /* definition of Oven_Table */
#include <zone_ui.h>     /* to get time <--> millisec conversions */
#include <exception.h>
#include <err_handler.h>
#include <glp.h>
#include <keyboard.h>


/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

#include "proto.h"
#include "pneu_int.h"

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/
/**begin_proto**/

#pragma SECTION PROG=rom3

/**end_proto**/



/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

UI_ERR ValidateSetVIPort( pINLET_STATUS status, PNEU_PORT_STATE state  )

/**end_proto**/
{
     if (  status->Config->Type != VOLATILES ) return NOT_COMPATIBLE ;
     if (state == CAPPED || state == PLUMBED ) return OK;
     if ((INT16)state == 0 ) return PARAM_TOO_SMALL;
     return PARAM_TOO_LARGE ;
}
/**begin_proto**/

UI_ERR SetVISplitPort ( pINLET_STATUS status, PNEU_PORT_STATE state )

/**end_proto**/
{
   UI_ERR error;

   if ( (error=ValidateSetVIPort(status, state )) == OK )
   {
       LockResource(   &Inst_Status.Pneu.Resource_Lock );
       status->Config->VI_Split_Port = state ;
       ReleaseResource( &Inst_Status.Pneu.Resource_Lock );

       if ( state == PLUMBED )
       {
          LogSetptChange( status->Position == FRNT_INLET ? F_INLET_VENT_ACTIVE : B_INLET_VENT_ACTIVE, 0, 0 );
       }
       else
       {
          LogSetptChange( status->Position == FRNT_INLET ? F_INLET_VENT_CAPPED  : B_INLET_VENT_CAPPED , 0, 0 );
       }
   }

   return error;

}


/**begin_proto**/

PNEU_PORT_STATE GetVISplitPort ( pINLET_STATUS status )

/**end_proto**/
{

   return status->Config->VI_Split_Port;

}

/**begin_proto**/

void SetDefaultVentConfig ( pINLET_STATUS status )

/**end_proto**/
{
       status->Config->VI_Split_Port = PLUMBED ;
}



/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

GAS_TYPE GetInletGasType( pINLET_STATUS status )

/**end_proto**/
{

   return status->Config->Gas_Type;

}


/**begin_proto**/

UI_ERR ValidateInletGasType( pINLET_STATUS status, GAS_TYPE gas )

/**end_proto**/
{

     switch ( gas )
     {
         case H2:
         case He:    return OK;

         case N2:
         case ArMe:   if ( status->Config->Type != VOLATILES  ) return OK;   /* until Sept 1996 ...  */

         case O2:
         case Air:   if ( ((INT16)status->Config->Type & 0x0f) == (INT16)JIB ) return OK;

                     /* intentional fall through */
         default:    return INVALID_PARAM;
     }
}

/**begin_proto**/

UI_ERR SetInletGasType( GAS_TYPE gas, pINLET_STATUS status )

/**end_proto**/
{
   UI_ERR error;

   if ( (error=ValidateInletGasType(status, gas)) == OK )
   {
       LockResource(   &Inst_Status.Pneu.Resource_Lock );
       error = ChangeInletGasType( gas, status );
       ReleaseResource( &Inst_Status.Pneu.Resource_Lock );
   }

   return error;
}

/**begin_proto**/

UI_ERR DcSetInletGasType( GAS_TYPE gas, pINLET_STATUS status )

/**end_proto**/
{
   UI_ERR error;

   if ( (error=ValidateInletGasType(status, gas)) == OK )
   {
       /* dont return caution to the workstation, they not are expecting */
       /* any error returns and will puke if they get any.               */

       (void)ChangeInletGasType( gas, status );
   }

   return error;
}


/**begin_proto**/

UI_ERR ChangeInletGasType( GAS_TYPE gas, pINLET_STATUS status )

/**end_proto**/
{
     status->Ready.Setpt_Change = TRUE;

     SendInletGasChangeRequest( status, gas );

     if ( !DoingMethodInstall() ) ReconfigurePneu();

     LogSetptChange( status->Position == FRNT_INLET ? F_INLET_GAS_TYPE : B_INLET_GAS_TYPE, 0, (INT32)gas );


     /* display a caution message if gas type is changed and user calib exits on an inlet with a flow sensor */
     if ((( status->Config->Type == SS )          ||
          ( status->Config->Type == PP )          ||
          ( status->Config->Type == ACI)          ||
          ( status->Config->Type == SIMDIST)      ||
          ( status->Config->Type == CIS3 )        ||
          ( status->Config->Type == CIS4 )        ||
          ( status->Config->Type == VOLATILES )   ||
          ( status->Config->Type == PTV ))   &&
        ( InletUserCalibState( status ) == USER_ENABLED )) return TEST_FAILED;

     return OK;

}
/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 GetInletAutoCal ( pINLET_STATUS status )

/**end_proto**/
{
    return status->Config->FS_Auto_Cal;
}

/**begin_proto**/

UI_ERR ValidateInletAutoCal( BIT8 mode, pINLET_STATUS status )

/**end_proto**/
{
      switch ( status->Config->Type )
      {
         case PP:
         case ACI:
         case SIMDIST:
         case CIS3:
         case CIS4:
         case VOLATILES:
         case PTV:
         case SS: if (( mode == TRUE ) || ( mode == FALSE ))  return OK;
                  else                                        return INVALID_PARAM;

         default:  return INVALID_PARAM;
      }
}

/**begin_proto**/

UI_ERR SetInletAutoCal ( BIT8 mode, pINLET_STATUS status )

/**end_proto**/
{
    UI_ERR error;

    if ( ( error = ValidateInletAutoCal( mode, status )) == OK )
    {

         ChangeInletAutoCal( mode, status );

         if ( mode == TRUE )
         {
            LogSetptChange( status->Position == FRNT_INLET ? F_INLET_AUTO_CAL_ON : B_INLET_AUTO_CAL_ON, 0, 0 );
         }
         else
         {
            LogSetptChange( status->Position == FRNT_INLET ? F_INLET_AUTO_CAL_OFF : B_INLET_AUTO_CAL_OFF, 0, 0 );
         }
    }

    return error;
}


/**begin_proto**/

void ChangeInletAutoCal ( BIT8 mode, pINLET_STATUS status )

/**end_proto**/
{
     status->Config->FS_Auto_Cal = mode;

     status->Ready.Setpt_Change = TRUE;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

ON_OFF GetInletMiserMode ( pINLET_STATUS status )

/**end_proto**/
{
    return status->Wkfile->Miser_Mode;
}

/**begin_proto**/

UI_ERR ValidateInletMiserMode( ON_OFF mode, pINLET_STATUS status )

/**end_proto**/
{
      switch ( VIGetInletType(status)  )
      {
         case CIS3:
         case CIS4:
         case SS:   if (( mode == ON ) || ( mode == OFF ))  return OK;
                    else                                    return INVALID_PARAM;

         default:  return INVALID_PARAM;
      }
}

/**begin_proto**/

void   FixMiserTime ( pINLET_STATUS status )

/**end_proto**/
{
    INT32 tym ;

            tym =  GetVIInjTime( status );
            if (
                 (  tym != 0                          ) &&
                 (  GetInletMiserTime ( status ) <=  tym  )
               )
               {
                ++tym;
                ChangeInletMiserTime ( tym, status ); /* Note that ChangeInletMiserTime call LogSetptChange ! */
/*              LogSetptChange( status->Position == 1? COL1_INIT_TIME: COL2_INIT_TIME, time, 0 ); see ChgColPresInitTime */
/*                        Note: SetptChg goes to GLP RUN_Log ONLY if in run or SCC_run !!                                */
/*                              so, ...   we need SetNewException HERE to get a dialog time pop-up warning to user !!    */
                SetNewException(status->Position==FRNT_INLET? FINL_PNEU_TIME1_CONFLICT : BINL_PNEU_TIME1_CONFLICT, tym ,0);
                ClearNewException(status->Position==FRNT_INLET? FINL_PNEU_TIME1_CONFLICT : BINL_PNEU_TIME1_CONFLICT);
               };
}




/**begin_proto**/

UI_ERR SetInletMiserMode ( ON_OFF mode, pINLET_STATUS status )

/**end_proto**/
{
    UI_ERR error;

    if ( ( error = ValidateInletMiserMode( mode, status )) == OK )
    {

         ChangeInletMiserMode( mode, status );

         if ( mode == ON )
         {
            LogSetptChange( status->Position == FRNT_INLET ? F_INLET_MISER_MODE_ON : B_INLET_MISER_MODE_ON, 0, 0 );
            if (  DoingMethodInstall()==0 ) FixMiserTime (status) ;
         }
         else
         {
            LogSetptChange( status->Position == FRNT_INLET ? F_INLET_MISER_MODE_OFF : B_INLET_MISER_MODE_OFF, 0, 0 );
         }
    }

    return error;
}


/**begin_proto**/

void ChangeInletMiserMode ( ON_OFF mode, pINLET_STATUS status )

/**end_proto**/
{
     INTERRUPTS_OFF
     status->Wkfile->Miser_Mode = mode;

     /* see if we need to turn off the led */
     if (( mode == OFF ) && (status->Miser_Mode == TRUE)) status->Miser_Mode = FALSE;
     INTERRUPTS_ON

     status->Ready.Setpt_Change = TRUE;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 GetInletFlowEquibTime ( pINLET_STATUS status )

/**end_proto**/
{
    return  MilliSecsToTime( status->Config->Flow_Equib_Time );
}

/**begin_proto**/

INT32 DcGetInletFlowEquibTime ( pINLET_STATUS status )

/**end_proto**/
{
    return  status->Config->Flow_Equib_Time;
}

/**begin_proto**/

UI_ERR ValidateInletFlowEquibTime ( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

     (void) status;

     return CheckTimeSetpt( time );

}


/**begin_proto**/

UI_ERR SetInletFlowEquibTime( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

    UI_ERR result;

    result =  ValidateInletFlowEquibTime( time, status );

    if ( result == OK )
    {
         ChangeInletFlowEquibTime( time, status );
    }

    return result;
}


/**begin_proto**/

UI_ERR DcSetInletFlowEquibTime( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

    UI_ERR result;

    result =  ValidateInletFlowEquibTime( time, status );

    if ( result == OK )
    {
         ChangeInletFlowEquibTime( time, status );
    }

    return result;
}


/**begin_proto**/

void ChangeInletFlowEquibTime( U_INT32 time, pINLET_STATUS status )

/**end_proto**/
{

     status->Config->Flow_Equib_Time = TimeToMilliSecs( time );

     LogSetptChange( status->Position == FRNT_INLET ? F_INLET_F_EQUIB_TIME : B_INLET_F_EQUIB_TIME, time, 0 );

     status->Ready.Setpt_Change = TRUE;
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 GetInletPresEquibTime ( pINLET_STATUS status )

/**end_proto**/
{
    return  MilliSecsToTime( status->Config->Pres_Equib_Time );
}

/**begin_proto**/

INT32 DcGetInletPresEquibTime ( pINLET_STATUS status )

/**end_proto**/
{
    return  status->Config->Pres_Equib_Time;
}

/**begin_proto**/

UI_ERR ValidateInletPresEquibTime ( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

     (void) time;
     (void) status;

     return CheckTimeSetpt( time );

}


/**begin_proto**/

UI_ERR SetInletPresEquibTime( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

    UI_ERR result;

    result =  ValidateInletPresEquibTime( time, status );

    if ( result == OK )
    {
         ChangeInletPresEquibTime( time, status );
    }

    return result;
}


/**begin_proto**/

UI_ERR DcSetInletPresEquibTime( INT32 time, pINLET_STATUS status )

/**end_proto**/
{

    UI_ERR result;

    result =  ValidateInletPresEquibTime( time, status );

    if ( result == OK )
    {
         ChangeInletPresEquibTime( time, status );
    }

    return result;
}


/**begin_proto**/

void ChangeInletPresEquibTime( U_INT32 time, pINLET_STATUS status )

/**end_proto**/
{

     status->Config->Pres_Equib_Time = TimeToMilliSecs( time );

     LogSetptChange( status->Position == FRNT_INLET ? F_INLET_P_EQUIB_TIME : B_INLET_P_EQUIB_TIME, time, 0 );

     status->Ready.Setpt_Change = TRUE;
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 GetInletPSensorRange( pINLET_STATUS status )

/**end_proto**/
{
    if ( status->Config->P_Sensor_Range == INVALID_PS_RANGE ) return 0;
    return status->Config->P_Sensor_Range;
}

/**begin_proto**/

INT32 GetDetPSensorRange( pDETECTOR_STATUS status, U_INT8 gas_num )

/**end_proto**/
{
    switch ( gas_num )
    {
        case 1: if (   status->Config->Gas_1.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                return status->Config->Gas_1.P_Sensor_Range;
        case 2: if (   status->Config->Gas_2.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                return status->Config->Gas_2.P_Sensor_Range;
        case 3: if (   status->Config->Gas_3.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                return status->Config->Gas_3.P_Sensor_Range;
    }

    return 0;
}

/**begin_proto**/

U_INT32 GetAuxPSensorRange( pAUX_STATUS status )

/**end_proto**/
{

      if ( !PneuAuxPresent() ) return 0;

      switch ( status->Position )
      {
         case AUX_3: if (   Inst_Config.Pneu.Aux_3.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                     return Inst_Config.Pneu.Aux_3.P_Sensor_Range;

         case AUX_4: if (   Inst_Config.Pneu.Aux_4.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                     return Inst_Config.Pneu.Aux_4.P_Sensor_Range;

         case AUX_5: if (   Inst_Config.Pneu.Aux_5.P_Sensor_Range == INVALID_PS_RANGE ) return 0;
                     return Inst_Config.Pneu.Aux_5.P_Sensor_Range;
      }

      return 0;
}

/**begin_proto**/

U_INT16 GetDetPresScale( pDETECTOR_STATUS status, U_INT8 gas_num )

/**end_proto**/
{
    switch ( gas_num )
    {
        case 1: return status->Gas_1.Dcm2_Per_IU;
        case 2: return status->Gas_2.Dcm2_Per_IU;
        case 3: return status->Gas_3.Dcm2_Per_IU;
    }

    return 0;
}

/**begin_proto**/

U_INT16 GetAuxPresScale( pAUX_STATUS status )

/**end_proto**/
{
    return status->Dcm2_Per_IU;
}

/**begin_proto**/

U_INT16 GetInletPresScale( pINLET_STATUS status )

/**end_proto**/
{

    return status->Dcm2_Per_IU;
}

/**begin_proto**/

U_INT32 GetInletFlowScale( pINLET_STATUS status )

/**end_proto**/
{

    return status->IU_Scaling;
}

/**begin_proto**/

U_INT32 GetDetFlowScale( pDETECTOR_STATUS status, U_INT8 gas_num )

/**end_proto**/
{
    switch ( gas_num )
    {
        case 1: return status->Gas_1.IU_Scaling;
        case 2: return status->Gas_2.IU_Scaling;
        case 3: return status->Gas_3.IU_Scaling;
    }

    return 0;
}


/**begin_proto**/

U_INT16 GetColumnPresScale( pCOLUMN_STATUS status )

/**end_proto**/
{

    return status->Dcm2_Per_IU;
}

/**begin_proto**/

U_INT32 GetColumnFlowScale( pCOLUMN_STATUS status )

/**end_proto**/
{

    return status->IU_Scaling;
}

/**begin_proto**/

#pragma SECTION UNDO

/**end_proto**/
